const Service = require('egg').Service;
class userService extends Service {

    updatesql(payload) {
        var sql = [];
        for (let key in payload) {
            if (Object.hasOwnProperty.call(payload, key) && key !== "id") {
                sql.push(`${key}="${payload[key]}"`);
            }
        }
        return sql.join(',');
    }
    async find(payload) {

        const TABLE_NAME = 'users';
        let sql = `select * from ${TABLE_NAME} where 1=1`;
        // console.log((sql))
        const users = await this.app.mysql.query(sql);

        return users;
    }
    async create(payload) {

        return await this.app.mysql.insert('users', {...payload });

    }
    async delete(id) {
        return await this.app.mysql.delete('users', { id: id })
    }
    async update(payload) {

        return await this.app.mysql.query(
            `update users set ${this.updatesql(payload)} where id="${payload.id}"`
        )

        // try {
        //     await this.app.mysql.query(
        //         `update users set ${this.updatesql(payload)} where id="${payload.id}"`
        //     )
        //     return true
        // } catch (error) {
        //     return false
        // }
    }

}

module.exports = userService;